package com.ryan.route.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.exception.TravelException;
import com.ryan.route.entity.DestCategory;
import com.ryan.route.entity.RouteDescription;
import com.ryan.route.entity.TravelRoute;
import com.ryan.route.entity.frontvo.RouteDetailVo;
import com.ryan.route.entity.frontvo.SearchRouteVo;
import com.ryan.route.entity.vo.RouteInfoVo;
import com.ryan.route.mapper.TravelRouteMapper;
import com.ryan.route.service.DestCategoryService;
import com.ryan.route.service.RouteDescriptionService;
import com.ryan.route.service.TravelRouteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryan.utils.vo.OrderRoute;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 * 旅游路线 服务实现类
 * </p>
 *
 * @author ryan
 * @since 2020-07-24
 */
@Service
public class TravelRouteServiceImpl extends ServiceImpl<TravelRouteMapper, TravelRoute> implements TravelRouteService {

    @Autowired
    private RouteDescriptionService descriptionService;

    @Autowired
    private DestCategoryService categoryService;

    /**
     * 添加路线基本信息
     * @param route
     * @return
     */
    @Override
    public String saveRouteInfo(TravelRoute route) {
        baseMapper.insert(route);
        return route.getId();
    }

    /**
     * 分页+条件查询所有路线
     * @param pageIndex
     * @param pageSize
     * @param title
     * @return
     */
    @Override
    public Map<String, Object> queryPage(long pageIndex, long pageSize, String title) {
        QueryWrapper<TravelRoute> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title", title);
        }
        wrapper.orderByDesc("gmt_create");
        Page<TravelRoute> page = new Page<>(pageIndex, pageSize);
        baseMapper.selectPage(page, wrapper);

        List<TravelRoute> records = page.getRecords();
        long total = page.getTotal();

        List<RouteInfoVo> routeInfoVoList = new ArrayList<>();
        for (TravelRoute record : records) {
            RouteInfoVo routeInfoVo = new RouteInfoVo();

            String destId = record.getDestId();
            DestCategory destCategory = categoryService.getById(destId);
            routeInfoVo.setId(record.getId());
            routeInfoVo.setCover(record.getCover());
            routeInfoVo.setDay(record.getDay());
            routeInfoVo.setGmtCreate(record.getGmtCreate());
            routeInfoVo.setNation(destCategory.getTitle());
            routeInfoVo.setPrice(record.getPrice());
            routeInfoVo.setStatus(record.getStatus());
            routeInfoVo.setTitle(record.getTitle());

            routeInfoVoList.add(routeInfoVo);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("list", routeInfoVoList);
        map.put("total", total);
        return map;
    }

    /**
     * 获取前台首页热门路线,4条
     * @return
     */
    @Override
    @Cacheable(value = "routeList", key = "'getIndexRoute'")//返回的数据保存到redis做缓存
    public List<TravelRoute> getIndexRoute() {
        QueryWrapper<TravelRoute> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("buy_count");
        wrapper.last("limit 4");
        List<TravelRoute> routeList = baseMapper.selectList(wrapper);
        return routeList;
    }

    /**
     * 前台页面条件分页查询
     * @param pageIndex
     * @param pageSize
     * @param searchRouteVo
     * @return
     */
    @Override
    public Map<String, Object> SearchRouteByVo(long pageIndex, long pageSize, SearchRouteVo searchRouteVo) {

        QueryWrapper<TravelRoute> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        Page<TravelRoute> routePage = new Page<>(pageIndex, pageSize);
        if (!StringUtils.isEmpty(searchRouteVo.getBuyCount())){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(searchRouteVo.getDestId())){
            wrapper.eq("dest_id",searchRouteVo.getDestId());
        }
        if(!StringUtils.isEmpty(searchRouteVo.getDestParentId())){
            wrapper.eq("dest_parent_id",searchRouteVo.getDestParentId());
        }
        if(!StringUtils.isEmpty(searchRouteVo.getGmtCreate())){
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(searchRouteVo.getPrice())){
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(routePage,wrapper);
        long total = routePage.getTotal();
        List<TravelRoute> records = routePage.getRecords();
        long current = routePage.getCurrent();
        long pages = routePage.getPages();
        long size = routePage.getSize();
        boolean hasPrevious = routePage.hasPrevious();
        boolean hasNext = routePage.hasNext();
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("list",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }

    /**
     * 通过id获取路线详情
     * @param routeId
     * @return
     */
    @Override
    public RouteDetailVo getRouteDetailById(String routeId) {

        RouteDetailVo route = baseMapper.getRouteDetailById(routeId);
        return route;
    }

    /**
     * 更新路线销量
     * @param orderRoute
     */
    @Override
    public void updateBuyCount(OrderRoute orderRoute) {
        TravelRoute route = baseMapper.selectById(orderRoute.getId());
        route.setBuyCount(orderRoute.getBuyCount());
        baseMapper.updateById(route);
    }

}
